package com.jeesite.modules.dataScreen.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jeesite.modules.dataScreen.domain.monitoring.RankVO;
import com.jeesite.modules.dataScreen.enums.DataTypeEnum;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import com.jeesite.modules.dataScreen.web.vo.citizenopinion.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * @author machenzhi
 * @date 2024/06/30 18:15
 */
@Service
public class CitizenOpinionService {

    @Autowired
    private IndustryIndexService industryIndexService;

    @Autowired
    private IndustryExecutor industryExecutor;

    private static final List<String> INDEX_ORDER_TREND = new ArrayList<>();


    static {
        INDEX_ORDER_TREND.add("7日工单量走势第7天");
        INDEX_ORDER_TREND.add("7日工单量走势第6天");
        INDEX_ORDER_TREND.add("7日工单量走势第5天");
        INDEX_ORDER_TREND.add("7日工单量走势第4天");
        INDEX_ORDER_TREND.add("7日工单量走势第3天");
        INDEX_ORDER_TREND.add("7日工单量走势第2天");
        INDEX_ORDER_TREND.add("7日工单量走势第1天");
    }

    @PostConstruct
    private void init(){
        Map<String, BiConsumer<CitizenOpinionDayVo, String>> specialConsumers = new HashMap<>();
        specialConsumers.put("民呼我为分区县好评率", (citizenOpinionDayVo, jsonData) -> {
            List<RankVO> rankVOS = jsonToRankVo(jsonData, 10);
            citizenOpinionDayVo.setDistrictPraiseRates(rankVOS);
        });

        specialConsumers.put("新增差评", (citizenOpinionDayVo, jsonData) -> {
            List<RankVO> rankVOS = jsonToRankVo(jsonData, 10);
            citizenOpinionDayVo.setBadRates(rankVOS);
        });
        IndustryExecutor.setSpecialConsumer(CitizenOpinionDayVo.class, specialConsumers);
    }


    public CitizenOpinionOneMinVo getOneMinCitizenOpinion() {
        CitizenOpinionOneMinVo citizenOpinionOneMinVo = new CitizenOpinionOneMinVo();
        industryExecutor.execute(citizenOpinionOneMinVo , DataTypeEnum.UNREAL_TIME.getCode());
        return citizenOpinionOneMinVo;
    }

    public CitizenOpinionFiveMinVo getFiveMinCitizenOpinion() {
        CitizenOpinionFiveMinVo citizenOpinionFiveMinVo = new CitizenOpinionFiveMinVo();
        industryExecutor.execute(citizenOpinionFiveMinVo , DataTypeEnum.UNREAL_TIME.getCode());
        return citizenOpinionFiveMinVo;
    }

    public CitizenOpinionHourVo getHourCitizenOpinion() {
        CitizenOpinionHourVo citizenOpinionHourVo = new CitizenOpinionHourVo();
        industryExecutor.execute(citizenOpinionHourVo , DataTypeEnum.UNREAL_TIME.getCode());
        return citizenOpinionHourVo;
    }


    public CitizenOpinionDayVo getDayCitizenOpinion() {
        CitizenOpinionDayVo citizenOpinionDayVo = new CitizenOpinionDayVo();
        industryExecutor.execute(citizenOpinionDayVo , DataTypeEnum.UNREAL_TIME.getCode());
        return citizenOpinionDayVo;
    }


    public List<RankVO> orderNumTrend() {
        Map<String, IndustryIndexVo> indexVoMap = industryIndexService.getUnRealtime(INDEX_ORDER_TREND.toArray(new String[0]));
        List<RankVO> rankVOS = new ArrayList<>();
        int index = 1;
        for (String key : INDEX_ORDER_TREND) {
            IndustryIndexVo industryIndexVo = indexVoMap.get(key);
            RankVO rankVO = new RankVO();
            rankVO.setIndex(index);
            rankVO.setText(key);
            rankVO.setValue(industryIndexVo.getIndicatorValue());
            rankVOS.add(rankVO);
            index++;
        }
        return rankVOS;
    }



    private static List<RankVO> jsonToRankVo(String jsonData, Integer limit) {
        if (StringUtils.isNotBlank(jsonData)) {
            JSONObject jsonObject = JSONObject.parseObject(jsonData);
            JSONArray jsonArray = jsonObject.getJSONArray("data");

            //将JSON数组转换为RankVO列表
            List<RankVO> rankList = jsonArray.toJavaList(JSONObject.class).stream()
                    .map(jsonObjectItem -> {
                        RankVO rankVO = new RankVO();
                        rankVO.setText(jsonObjectItem.getString("name"));
                        rankVO.setValue(jsonObjectItem.getString("content"));
                        return rankVO;
                    })
                    .limit(limit)
                    .collect(Collectors.toList());

            // 设置序号index
            for (int i = 0; i < rankList.size(); i++) {
                rankList.get(i).setIndex(i + 1);
            }
            return rankList;
        }
        return Collections.emptyList();
    }

}
